<?php

use think\facade\Db;


function getBonusTitle($type)
{
    if ($type == 1) {
        return [1 => '直推收益', 2 => '间推收益' ,3 => '拍照上传'];
    } else {
        return ['sum_1' => '直推收益', 'sum_2' => '间推收益', 'sum_3' => '拍照上传'];
    }

}

function getCount($fetchSql)
{
    $fetchSql = "SELECT count(*) as tp_count " . strstr($fetchSql, "FROM");
    return Db::query($fetchSql)[0]['tp_count'];
}

function get_pay_way($value)
{
    switch ($value) {
        case "":
            return '-';
        case "alipay":
            return '支付宝';
        case "wechat":
            return '微信支付';
        default:
            $assets = \app\common\model\Assets::getFields(3);
            return $assets[$value];
    }
}

function getOrderStatus($value, $type = 1)
{
    if ($type == 1) {
        switch ($value) {
            case 1:
                return '待付款';
            case 2:
                return '待发货';
            case 3:
                return '待收货';
            case 4:
                return '已完成';
            case 5:
                return '已取消';
            default:
                return '其他';
        }
    } else {
        return [1 => '待付款', 2 => '待发货', 3 => '待收货', 4 => '已完成', 5 => '已取消'];
    }

}


function dsubstr($string, $length, $suffix = '', $start = 0) {

    if($start) {
        $tmp = dsubstr($string, $start);
        $string = substr($string, strlen($tmp));
    }
    $strlen = strlen($string);
    if($strlen <= $length) return $string;
    $string = str_replace(array('"', '<', '>'), array('"', '<', '>'), $string);
    $length = $length - strlen($suffix);
    $str = '';
    if('UTF-8' == 'UTF-8') {
        $n = $tn = $noc = 0;
        while($n < $strlen) {
            $t = ord($string{$n});
            if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                $tn = 1; $n++; $noc++;
            } elseif(194 <= $t && $t <= 223) {
                $tn = 2; $n += 2; $noc += 2;
            } elseif(224 <= $t && $t <= 239) {
                $tn = 3; $n += 3; $noc += 2;
            } elseif(240 <= $t && $t <= 247) {
                $tn = 4; $n += 4; $noc += 2;
            } elseif(248 <= $t && $t <= 251) {
                $tn = 5; $n += 5; $noc += 2;
            } elseif($t == 252 || $t == 253) {
                $tn = 6; $n += 6; $noc += 2;
            } else {
                $n++;
            }
            if($noc >= $length) break;
        }
        if($noc > $length) $n -= $tn;
        $str = substr($string, 0, $n);
    } else {
        for($i = 0; $i < $length; $i++) {
            $str .= ord($string{$i}) > 127 ? $string{$i}.$string{++$i} : $string{$i};
        }
    }
    $str = str_replace(array('"', '<', '>'), array('"', '<', '>'), $str);
    return $str == $string ? $str : $str.$suffix;
}


function getOperation($name)
{
    switch ($name) {
        case "save":
            return '修改';
        case "create":
            return '新增';
        case "update":
            return '更新状态';
        case "updateAll":
            return '批量更新状态';
        case "delete":
            return '删除';
        case "deleteAll":
            return '批量删除';
        default:
            return '操作';
    }
}

function getNotifyType($name)
{
    switch ($name) {
        case 1:
            return '订单通知';
        case 2:
            return '注册通知';
        default:
            return '系统通知';
    }
}

/**
 * 格式化字节大小
 * @param number $size 字节数
 * @param string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '')
{
    $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
    for ($i = 0; $size >= 1024 && $i < 5; $i++) {
        $size /= 1024;
    }
    return $size . $delimiter . $units[$i];
}


function generate($length, $prefix = '', $type = 1)
{
    if ($type == 1) {
        $code = rand(100000, 999999);
    } else {
        $chars = array('a', 'b', '1', 'd', 'e', '2', 'g', 'h',
            'i', '3', 'k', 'l', '4', 'n', 'o', '5', 'q', 'r', 's',
            't', 'u', '6', 'w', 'x', '7', 'z', 'c', '8', 'j', 'm',
            'p', 'v', 'y', 'f', '9');
        $keys = array_rand($chars, $length);
        $code = '';
        for ($i = 0; $i < $length; $i++) {
            $code .= $chars[$keys[$i]];
        }
    }

    if ($prefix) {
        $code = $prefix . $code;
    }

    $is = \app\common\model\User::where('code', $code)->count();
    if ($is) {
        return generate(6, $prefix, $type);
    } else {
        return $code;
    }

}

//格式化富文本内容
function handleRichText($content, $http = 'http')
{
    $url = $http . '://' . $_SERVER['SERVER_NAME'] . '/uploads/';
    $content = htmlspecialchars_decode(str_replace('/uploads/', $url, $content));
    return str_replace('\\', '', $content);
}

function handleImg($src, $http = 'http')
{
    if (empty($src)) {
        return '';
    }
    if (is_array($src)) {
        foreach ($src as &$v) {
            $v = $http . '://' . $_SERVER['SERVER_NAME'] . $v;
        }
        return $src;
    } else {
        return $http . '://' . $_SERVER['SERVER_NAME'] . $src;
    }

}

function filter_Emoji($str)
{
    $str = preg_replace_callback(    //执行一个正则表达式搜索并且使用一个回调进行替换
        '/./u',
        function (array $match) {
            return strlen($match[0]) >= 4 ? '' : $match[0];
        },
        $str);

    return $str;
}

function queryIp($ip)
{

    $url = "http://whois.pconline.com.cn/jsFunction.jsp?ip=" . $ip;
    $ch = curl_init();
    //设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    //执行并获取HTML文档内容
    $output = curl_exec($ch);
    //释放curl句柄
    curl_close($ch);
    $info = iconv('GB2312', 'UTF-8', $output); //因为是js调用 所以接收到的信息为字符串，注意编码格式

    return get_between($info, "('", "',");
}

function get_between($input, $start, $end)
{
    $substr = substr($input, strlen($start) + strpos($input, $start), (strlen($input) - strpos($input, $end)) * (-1));
    return $substr;
}

